package com.onlyforfun.exchange.dao;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.onlyforfun.exchange.domain.TimePrice;
import com.onlyforfun.exchange.util.NumberParse;
import com.onlyforfun.exchange.util.Utils;

public class BocExchangeDao implements ExchangeDao {

	/* (non-Javadoc)
	 * @see com.onlyforfun.exchange.dao.ExchangeDao#findByCurrencyName(java.util.Date, java.util.Date, java.lang.String, int)
	 */
	@Override
	public List<TimePrice> findByCurrencyName(Date start, Date end, String currencyName, int pageNo) {
		List<TimePrice> timePrices = new ArrayList<TimePrice>();
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
			Response res = Jsoup.connect("http://srh.bankofchina.com/search/whpj/search.jsp")
					.data("erectDate", df.format(start), "nothing", df.format(end),
							"pjname", currencyName, "page", String.valueOf(pageNo))
							.timeout(timeout)
							.method(Method.POST).execute();
			Document doc = res.parse();
			Elements datalist = doc.body().getElementsByTag("table");
			
			if (datalist.size() <= 1)
				return timePrices;
			
			Element data = datalist.get(2);
			Elements trs = data.getElementsByTag("tr");
			for (int i = 1; i < trs.size(); i++) {
				TimePrice timePrice = new TimePrice();
				Elements tds = trs.get(i).getElementsByTag("td");
				timePrice.setBank(0);
				timePrice.setName(tds.get(0).text());
				timePrice.setSpotPurchasePrice(NumberParse.parseMoneyDouble(tds.get(1).text()));
				timePrice.setCashPurchasePrice(NumberParse.parseMoneyDouble(tds.get(2).text()));
				timePrice.setSpotOfferPrice(NumberParse.parseMoneyDouble(tds.get(3).text()));
				timePrice.setCashOfferPrice(NumberParse.parseMoneyDouble(tds.get(4).text()));
				timePrice.setSafePrice(NumberParse.parseMoneyDouble(tds.get(5).text()));
				timePrice.setBocDiscountedPrice(NumberParse.parseMoneyDouble(tds.get(6).text()));
				
				df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.CHINA);
				timePrice.setPublicTime(df.parse(tds.get(7).text()));
				timePrices.add(timePrice);
			}
			
		} catch (Exception e) {
//			throw new RuntimeException(e);
		}
		return timePrices;
	}
	
	/* (non-Javadoc)
	 * @see com.onlyforfun.exchange.dao.ExchangeDao#findByCurrencyName(java.util.Date, java.util.Date, java.lang.String)
	 */
	@Override
	public List<TimePrice> findByCurrencyName(Date start, Date end, String currencyName) {
		List<TimePrice> timePrices = new ArrayList<TimePrice>();
		List<TimePrice> tmptimePrices = findByCurrencyName(start, end,
				currencyName, 1);
		int pageNo = 2;
		timePrices.addAll(tmptimePrices);
		while (tmptimePrices.size()>=20) {
			tmptimePrices = findByCurrencyName(start, end,
					currencyName, pageNo);
			if (tmptimePrices.size() == 20 
					&& tmptimePrices.get(19).getPublicTime().equals(timePrices.get(timePrices.size() - 1).getPublicTime())) {
				break;
			}
			pageNo++;
			timePrices.addAll(tmptimePrices);
		}
		return timePrices;
	}

	/* (non-Javadoc)
	 * @see com.onlyforfun.exchange.dao.ExchangeDao#findByCurrencyNow(android.content.Context)
	 */
	@Override
	public List<TimePrice> findByCurrencyNow(Context context) {
		List<TimePrice> timePrices = new ArrayList<TimePrice>();
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
			Response res = Jsoup.connect("http://www.boc.cn/sourcedb/whpj/index.html?fetch=" + (new Date()).getTime()).method(Method.GET).timeout(timeout).execute();
			Document doc = res.parse();
			Elements datalist = doc.body().getElementsByTag("table");
			if (datalist.size() <= 6)
				return timePrices;
			
			Element data = datalist.get(7);
			Elements trs = data.getElementsByTag("tr");
			for (int i = 1; i < trs.size(); i++) {
				TimePrice timePrice = new TimePrice();
				timePrice.setBank(0);
				Elements tds = trs.get(i).getElementsByTag("td");
				timePrice.setName(tds.get(0).text());
				timePrice.setSpotPurchasePrice(NumberParse.parseMoneyDouble(tds.get(1).text()));
				timePrice.setCashPurchasePrice(NumberParse.parseMoneyDouble(tds.get(2).text()));
				timePrice.setSpotOfferPrice(NumberParse.parseMoneyDouble(tds.get(3).text()));
				timePrice.setCashOfferPrice(NumberParse.parseMoneyDouble(tds.get(4).text()));
//				timePrice.setSafePrice(NumberParse.parseDouble(tds.get(5).text()));
				timePrice.setBocDiscountedPrice(NumberParse.parseMoneyDouble(tds.get(5).text()));
				
				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
				timePrice.setPublicTime(df.parse(tds.get(6).text() + " " + tds.get(7).text()));
				timePrices.add(timePrice);
			}
			
		} catch (Exception e) {
//			throw new RuntimeException(e);
		}
		List<String> sortkeys = SortKeeper.readSortkeys(context);
		return Utils.resort(timePrices, sortkeys);
	}
	
}
